﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using QLNhaHangTiecCuoi.Models;

namespace QLNhaHangTiecCuoi.Controllers
{
    public class MonAnsController : Controller
    {
        private QLNhaHangTCEntities db = new QLNhaHangTCEntities();

        // GET: MonAns
        public async Task<ActionResult> Index()
        {
            var monAns = db.MonAns.Include(m => m.LoaiMonAn);
            return View(await monAns.ToListAsync());
        }

        // GET: MonAns/Details/5
        public async Task<ActionResult> Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MonAn monAn = await db.MonAns.FindAsync(id);
            if (monAn == null)
            {
                return HttpNotFound();
            }
            return View(monAn);
        }
        public string CreateMaMA()
        {
            var item = db.MonAns.OrderByDescending(i => i.MaMonAn).FirstOrDefault();
            string ma;
            if (item == null) { ma = "MA0000"; }
            else ma=item.MaMonAn;
            int so = Convert.ToInt32(ma.Substring(2, 4));
            so++;
            string MaMA = "MA" + so.ToString("0000");
            return MaMA;
        }
        // GET: MonAns/Create
        public ActionResult Create()
        {
            ViewBag.ChonLoaiMonAn = GetLoaiMonAn();
            //ViewBag.MaLoaiMonAn = new SelectList(db.LoaiMonAns, "MaLoaiMonAn", "TenLoaiMonAn");
            return View();
        }

        // POST: MonAns/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "MaMonAn,TenMonAn,MoTa,MaLoaiMonAn,Gia,HinhAnh")] MonAn monAn)
        {
            MonAn MA = new MonAn()
            {
                MaMonAn = CreateMaMA(),
                TenMonAn = monAn.TenMonAn,
                MaLoaiMonAn = monAn.MaLoaiMonAn,
                Gia = monAn.Gia,
                HinhAnh = monAn.HinhAnh,
                MoTa = monAn.MoTa
                
            };
            if (ModelState.IsValid)
            {
                db.MonAns.Add(MA);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.ChonLoaiMonAn = GetLoaiMonAn();
            //ViewBag.MaLoaiMonAn = new SelectList(db.LoaiMonAns, "MaLoaiMonAn", "TenLoaiMonAn", monAn.MaLoaiMonAn);
            return View(monAn);
        }

        // GET: MonAns/Edit/5
        public async Task<ActionResult> Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MonAn monAn = await db.MonAns.FindAsync(id);
            if (monAn == null)
            {
                return HttpNotFound();
            }
            ViewBag.ChonLoaiMonAn = GetLoaiMonAn();
            //ViewBag.MaLoaiMonAn = new SelectList(db.LoaiMonAns, "MaLoaiMonAn", "TenLoaiMonAn", monAn.MaLoaiMonAn);
            return View(monAn);
        }

        // POST: MonAns/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "MaMonAn,TenMonAn,MoTa,MaLoaiMonAn,Gia,HinhAnh")] MonAn monAn)
        {
            if (ModelState.IsValid)
            {
                db.Entry(monAn).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.ChonLoaiMonAn = GetLoaiMonAn();
            //ViewBag.MaLoaiMonAn = new SelectList(db.LoaiMonAns, "MaLoaiMonAn", "TenLoaiMonAn", monAn.MaLoaiMonAn);
            return View(monAn);
        }

        // GET: MonAns/Delete/5
        public async Task<ActionResult> Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            MonAn monAn = await db.MonAns.FindAsync(id);
            if (monAn == null)
            {
                return HttpNotFound();
            }
            return View(monAn);
        }

        // POST: MonAns/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(string id)
        {
            MonAn monAn = await db.MonAns.FindAsync(id);
            db.MonAns.Remove(monAn);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        private List<SelectListItem> GetLoaiMonAn()
        {
            var x = (from n in db.LoaiMonAns select n);
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var item in x)
            {
                items.Add(new SelectListItem { Text = item.TenLoaiMonAn, Value = item.MaLoaiMonAn });
            }
            return items;
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
